package io.renren.modules.skate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.constant.Constant;
import io.renren.common.utils.DateUtils;
import io.renren.modules.skate.dao.FAthleteDao;
import io.renren.modules.skate.dto.FAthleteDTO;
import io.renren.modules.skate.entity.FAthleteEntity;
import io.renren.modules.skate.excel.FAthleteExcel;
import io.renren.modules.skate.service.FAthleteService;
import io.renren.modules.security.user.SecurityUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * 运动员表
 *
 * @author Mark sunlightcs@gmail.com
 * @since 3.0 2023-11-28
 */
@Service
public class FAthleteServiceImpl extends CrudServiceImpl<FAthleteDao, FAthleteEntity, FAthleteDTO> implements FAthleteService {

    @Override
    public QueryWrapper<FAthleteEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<FAthleteEntity> wrapper = new QueryWrapper<>();

        String username = (String)params.get("username");
        String idCard = (String)params.get("idCard");
        String flag = (String)params.get("flag");
        Object clubId = (Object)params.get("clubId");
        //String strClubId = clubId!=null ? clubId.toString() : null;
        if(StringUtils.isNotBlank(username)){
            wrapper.like("username", username);
        }
        if(StringUtils.isNotBlank(idCard)){
            wrapper.like("id_card", idCard);
        }
        if(StringUtils.isNotEmpty(flag)){
            wrapper.eq("flag", flag);
        }
        if(clubId != null){
            wrapper.eq("club_id", clubId);
        }
        return wrapper;
    }

    @Override
    public void batchRenZheng(Long[] ids){
        Date nextYear = DateUtils.addDateYears(new Date(), 1);
        nextYear = DateUtils.addDateDays(nextYear, -1);
        for (Long id : ids) {
            FAthleteEntity fAthleteEntity = baseDao.selectById(id);
            fAthleteEntity.setFlag("1");
            fAthleteEntity.setExpirationDate(nextYear);
            fAthleteEntity.setUpdater(SecurityUser.getUserId().toString());
            fAthleteEntity.setUpdateDate(new Date());
            baseDao.updateById(fAthleteEntity);
        }
    }

    // 取最大编号
    @Override
    public String getMaxNo(){
        return baseDao.getMaxNo();
    }

    @Override
    public void checkData(FAthleteExcel excel, int index){
        // 检查身份证号是否重复
        String idCard = excel.getIdCard();
        FAthleteEntity fAthleteEntity = baseDao.selectOne(
                new QueryWrapper<FAthleteEntity>()
                        .eq("id_card", idCard)
                        .last("limit 1"));
        if(fAthleteEntity != null){
            excel.setCheckMsg("第"+index+"行，"+excel.getIdCard()+"身份证号已存在");
        }
        // 检查手机号是否重复
        String phone = excel.getPhone();
        fAthleteEntity = baseDao.selectOne(
                new QueryWrapper<FAthleteEntity>()
                        .eq("phone", phone)
                        .last("limit 1"));
        if(fAthleteEntity != null){
            excel.setCheckMsg("第"+index+"行，"+excel.getPhone()+"手机号已存在");
        }
    }

}